//
//  SPStateCell.m
//  Sport
//
//  Created by Cass on 2018/1/11.
//  Copyright © 2018年 Cass. All rights reserved.
//

#import "SPStateCell.h"
#import "SPHomeStateModel.h"
#import "ZPImageMagnification.h"
#import "SPtime.h"
#define MAXWidth [UIScreen mainScreen].bounds.size.width-250
#define MAXHeight 100
#define MarginLeftRight 12
@interface SPStateCell()

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *locLabel;
@property (weak, nonatomic) UILabel *contentLabel;
@property (weak, nonatomic) IBOutlet UIView *bottomView;

@property (nonatomic, strong) NSMutableArray *imagesArray;
@property (nonatomic, strong) NSMutableArray *imageViewsArray;
@property (weak, nonatomic) IBOutlet UIButton *zanBtn;


@end

@implementation SPStateCell


- (void)awakeFromNib {
    [super awakeFromNib];
    
}
- (NSMutableArray *)imagesArray{
    if (!_imagesArray) {
        _imagesArray = [NSMutableArray array];
    }
    return _imagesArray;
}
-(NSMutableArray *)imageViewsArray{
    if (!_imageViewsArray) {
        _imageViewsArray = [NSMutableArray array];
    }
    return _imageViewsArray;
}
- (void)layoutSubviews{
    [super layoutSubviews];
}
- (void)bigImage:(UITapGestureRecognizer *)gesture{
    [ZPImageMagnification scanBigImageWithImageView:(UIImageView *)gesture.self.view alpha:1];
}

- (void)zanNumber{
    [self.supportBtn setTitle:[NSString stringWithFormat:@"点赞 1"] forState:UIControlStateNormal];
}
- (void)setModel:(SPHomeStateModel *)model{
    _model = model;
    if (model.photo.length) {
        NSArray *images = [model.photo componentsSeparatedByString:@"-"];
        for (NSString *url in images) {
            [self.imagesArray addObject:url];
        }
    }
    
    [self.headImg sd_setImageWithURL:[NSURL URLWithString:model.headimage] placeholderImage:[UIImage imageNamed:@"ha"]];
    self.headImg.layer.cornerRadius = self.headImg.bounds.size.width / 2;
    self.headImg.layer.masksToBounds = YES;
    self.nameLabel.text = model.nickname;
    self.timeLabel.text = [[SPtime sharedTime]timeWithCreatTime: [model.datetime substringToIndex:16]];
    self.locLabel.text = model.address;
    
    [self.zanBtn addTarget:self action:@selector(zanNumber) forControlEvents:UIControlEventTouchUpInside];
    [self.commentBtn setTitle:[NSString stringWithFormat:@"评论 %@",model.countnumber] forState:UIControlStateNormal];
    UILabel *contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.headImg.frame), CGRectGetMaxY(self.headImg.frame) + 10, SCREEN_WIDTH - 2 * MarginLeftRight, 40)];
    contentLabel.text = model.message;
    contentLabel.textColor = [UIColor blackColor];
    contentLabel.font = [UIFont systemFontOfSize:15];
    contentLabel.numberOfLines = 0;
    contentLabel.lineBreakMode = NSLineBreakByWordWrapping;
    [contentLabel sizeToFit];
    [self.contentView addSubview:contentLabel];
    self.contentLabel = contentLabel;
    
    if (self.imagesArray.count == 0) {
        self.bottomView.frame = CGRectMake(0, CGRectGetMaxY(self.contentLabel.frame) + 10, SCREEN_WIDTH, 44);
    }else if (self.imagesArray.count == 1) {
        CGFloat imageViewWH = (SCREEN_WIDTH - 3 * MarginLeftRight) / 3;
        UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.contentLabel.frame), CGRectGetMaxY(self.contentLabel.frame) + 10, MAXWidth, MAXHeight)];
        imageview.backgroundColor = [UIColor whiteColor];
//        [imageview setImage:[UIImage imageNamed:self.imagesArray[0]]];
        [imageview sd_setImageWithURL:self.imagesArray[0] placeholderImage:[UIImage imageNamed:@"ha"]];
        
        //    [imageview.image scaleImageWithSize:CGSizeMake(MAXWidth, MAXHeight)];
        //    [imageview sizeToFit];
        UITapGestureRecognizer *tag1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(bigImage:)];
        [imageview addGestureRecognizer:tag1];
        imageview.userInteractionEnabled = YES;
        [self.contentView addSubview:imageview];
        [self.imageViewsArray addObject:imageview];
        self.bottomView.frame = CGRectMake(0, CGRectGetMaxY(imageview.frame) + 10, SCREEN_WIDTH, 44);
    }else if (self.imagesArray.count == 2){
        CGFloat imageViewWH = (SCREEN_WIDTH - 3 * MarginLeftRight) / 3;
        for (NSInteger i = 0; i < self.imagesArray.count; i++) {
            UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(MarginLeftRight + (i % 2) * (imageViewWH + MarginLeftRight), CGRectGetMaxY(contentLabel.frame) + 10, imageViewWH, imageViewWH)];
            imageview.backgroundColor = [UIColor whiteColor];
//            [imageview setImage:[UIImage imageNamed:self.imagesArray[i]]];
            [imageview sd_setImageWithURL:self.imagesArray[i] placeholderImage:[UIImage imageNamed:@"ha"]];
            UITapGestureRecognizer *tag1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(bigImage:)];
            [imageview addGestureRecognizer:tag1];
            imageview.userInteractionEnabled = YES;
            [self.contentView addSubview:imageview];
            [self.imageViewsArray addObject:imageview];
            self.bottomView.frame = CGRectMake(0, CGRectGetMaxY(imageview.frame) + 10, SCREEN_WIDTH, 44);
        }
    }else{
        CGFloat imageViewWH = (SCREEN_WIDTH - 4 * MarginLeftRight) / 3;
        for (NSInteger i = 0; i < self.imagesArray.count; i++) {
            UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(MarginLeftRight + (i % 3) * (imageViewWH + MarginLeftRight), CGRectGetMaxY(contentLabel.frame) + 10 + (i / 3) * (imageViewWH + MarginLeftRight), imageViewWH, imageViewWH)];
            imageview.backgroundColor = [UIColor whiteColor];
//            [imageview setImage:[UIImage imageNamed:self.imagesArray[i]]];
            [imageview sd_setImageWithURL:self.imagesArray[i] placeholderImage:[UIImage imageNamed:@"ha"]];
            UITapGestureRecognizer *tag1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(bigImage:)];
            [imageview addGestureRecognizer:tag1];
            imageview.userInteractionEnabled = YES;
            [self.contentView addSubview:imageview];
            [self.imageViewsArray addObject:imageview];
            self.bottomView.frame = CGRectMake(0, CGRectGetMaxY(imageview.frame), SCREEN_WIDTH, 44);
        }
    }
    
    model.cellHeight = CGRectGetMaxY(self.bottomView.frame) + 10;
    [self layoutIfNeeded];
    
    
}
+(instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString *ID = @"cell";
    SPStateCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([SPStateCell class]) owner:nil options:nil]lastObject];
    }
    return cell;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}

@end
